/*
 * @Author: 杨三炮
 * @Date: 2021-11-23 20:04:27
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-11-24 14:42:40
 * @FilePath: \node\node\model\teacher_mod.js
 */
module.exports = class teacher_mod extends require('./model') {
    /**
      * @description: 数据库登录
      * @param {*} username
      * @param {*} password
      * @param {*} type
      * @return {*}
      */

    static searchUserMod (u_id) {
        return new Promise((resolve, reject) => {
            let sql = `select *from teacher where id='${u_id}'`
            this.query(sql).then(result => {
                if (!result.length) {
                    resolve(0)
                    return
                }
                resolve(result[0])
            }).catch(err => {
                reject(err)
            })
        })
    }

    static upPwdMod (u_id, oldPwd, newPwd, user) {
        if (user.password != oldPwd) {
            return { code: 201, msg: '旧密码不匹配' }
        }
        return new Promise((resolve, reject) => {
            let sql = "UPDATE teacher SET `password`=? WHERE id=?"
            this.query(sql, this.formatParams(newPwd, u_id)).then(result => {
                resolve({
                    code: 200,
                    msg: '修改成功'
                })
            }).catch(err => {
                reject(err)
            })
        })
    }

    static getUserMod () {
        return new Promise((resolve, reject) => {
            let sql = `select *from teacher as t order by modifytime desc ;`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
    * @description: 用户信息修改
    * @param {*} data
    * @return {*}
    */
    static upUserdataMod (data) {
        return new Promise((resolve, reject) => {
            let sql = `UPDATE teacher SET username = ?,sex=?,address=?,modifytime=NOW() WHERE id = ?;`
            let params = [data.username, data.sex, data.address, data.u_id]
            this.query(sql, params).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
     * @description: 插入用户
     * @param {*} data
     * @return {*}
     */
    static setXlsxDataMod (data) {
        return new Promise((resolve, reject) => {
            let sql = `INSERT INTO teacher VALUES(?,?,?,NULL,NULL,?,?,NOW(),NOW(),?);`
            let params = [data.u_id, data.username, data.password, data.address, data.sex, data.class_id]
            this.query(sql, params).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }

    /**
   * @description: 根据用户id删除用户 删除成功返回1
   * @param {*} u_id
   * @return {*}返回1
   */
    static delUserdataMod (u_id) {
        return new Promise((resolve, reject) => {
            let sql = `DELETE FROM teacher WHERE id=${u_id};`
            this.query(sql).then(result => {
                resolve({
                    msg: '删除成功',
                    code: 200
                })
            }).catch(err => {
                reject(err)
            })
        })
    }


    static teacherClassMod () {
        return new Promise((resolve, reject) => {
            let sql = `SELECT * FROM teacher as t
            INNER JOIN teacherorclass as tc
            ON t.id=tc.t_id
            INNER JOIN class as c
            ON tc.c_id=c.c_id;`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }
    static getClassLeaveMod (u_id) {
        return new Promise((resolve, reject) => {
            let sql = `SELECT *FROM teacher as t
            INNER JOIN teacherorclass as tc
            ON t.id=tc.t_id
            INNER JOIN student as s
            ON tc.c_id=s.class_id
            INNER JOIN`+ " `leave` " + `as l
            ON l.u_id=s.id
            INNER JOIN class AS c
            ON c.c_id=tc.c_id
            WHERE t.id=${u_id};`
            this.query(sql).then(result => {
                resolve(result)
            }).catch(err => {
                reject(err)
            })
        })
    }
}